package com.wangtao.firstapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.RelationBrandAdatper;
import com.wangtao.firstapp.adapters.RelationProductAdatper;
import com.wangtao.firstapp.modles.RelationBrandData;
import com.wangtao.firstapp.modles.RelationProductData;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 写文章页面  底部  点击商品按钮后跳转到该页面
 * Created by lanouhn on 16/9/14.
 */
public class RelationProductActivity extends AppCompatActivity {
    private ImageView iv_back_titlebar_relationproduct;// 标题栏左上角返回按钮
    private EditText et_brandname_relationproduct;// 输入品牌名的EditText
    private EditText et_productname_relationproduct;// 输入商品名的EditText
    private ListView listview_relationproduct;// 显示搜索的商品的ListView
    private RelationBrandData brandData;// 通过品牌名搜索时的实体类
    private RelationProductData productData;// 通过商品名搜索时的实体类
    private RelationBrandAdatper brandAdatper;// 通过品牌名搜索时的适配器
    private RelationProductAdatper productAdatper;// 通过商品名搜索时的适配器

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relationproduct);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        iv_back_titlebar_relationproduct = (ImageView) findViewById(R.id.iv_back_titlebar_relationproduct);
        et_brandname_relationproduct = (EditText) findViewById(R.id.et_brandname_relationproduct);
        et_productname_relationproduct = (EditText) findViewById(R.id.et_productname_relationproduct);
        listview_relationproduct = (ListView) findViewById(R.id.listview_relationproduct);
    }

    private void initData() {
        et_brandname_relationproduct.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String brandname = et_brandname_relationproduct.getText().toString();
                // 通过品牌名搜索
                if (brandname.length() > 0) {
                    String url = "http://open4.bantangapp.com/search/brand/listByKeyword";
                    String param = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=sougou&app_installtime=1471923362&app_versions=5.8.8&os_versions=5.1&screensize=1080&v=19&page=0&pagesize=30&keyword=" + brandname;
                    try {
                        HttpUtils.doPostAsyn(url, param, new HttpUtils.CallBack() {
                            @Override
                            public void onRequestComplete(String result) {
                                Gson gson = new Gson();
                                brandData = gson.fromJson(result, RelationBrandData.class);
                                if (brandData.getData().size() > 0) {
                                    handler.sendEmptyMessage(1);
                                } else {
                                    handler.sendEmptyMessage(0);
                                }
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else if (brandname.length() == 0) {
                    listview_relationproduct.setAdapter(null);
                }
            }
        });

        et_productname_relationproduct.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String productname = et_productname_relationproduct.getText().toString();
                // 通过商品名搜索
                if (productname.length() > 0) {
                    String url1 = "http://open4.bantangapp.com/search/brandProduct/listByKeyword";
                    String param1 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=sougou&app_installtime=1471923362&app_versions=5.8.8&os_versions=5.1&screensize=1080&v=19&page=0&pagesize=30&brand_id=&keyword=" + productname;
                    try {
                        HttpUtils.doPostAsyn(url1, param1, new HttpUtils.CallBack() {
                            @Override
                            public void onRequestComplete(String result) {
                                Gson gson = new Gson();
                                productData = gson.fromJson(result, RelationProductData.class);
                                if (productData.getData().size() > 0) {
                                    handler.sendEmptyMessage(2);
                                } else {
                                    handler.sendEmptyMessage(0);
                                }
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else if (productname.length() == 0) {
                    listview_relationproduct.setAdapter(null);
                }
            }
        });
    }

    private void initEvent() {
        // 点击返回按钮
        iv_back_titlebar_relationproduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                    break;
                case 1:
                    brandAdatper = new RelationBrandAdatper(RelationProductActivity.this, brandData);
                    listview_relationproduct.setAdapter(brandAdatper);
                    break;
                case 2:
                    productAdatper = new RelationProductAdatper(RelationProductActivity.this, productData);
                    listview_relationproduct.setAdapter(productAdatper);
                    break;
            }
        }
    };

}
